package com.atguigu.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.servicebase.exceptionhandler.GuLiException;
import com.atguigu.serviceedu.entity.EduSubject;
import com.atguigu.serviceedu.entity.excel.SubjectData;
import com.atguigu.serviceedu.entity.subject.OneSubject;
import com.atguigu.serviceedu.entity.subject.TwoSubject;
import com.atguigu.serviceedu.listener.SubjectExcelListener;
import com.atguigu.serviceedu.mapper.EduSubjectMapper;
import com.atguigu.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author jyd
 * @since 2022-03-19
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try{
            //以流的方式读取excel表的数据
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            throw new GuLiException(20001,"添加课程分类失败");
        }


    }

    @Override
    public List<OneSubject> getSubjectList() {
        //获取一级分类数据
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> oneSubjects = baseMapper.selectList(wrapper);
        //获取二级分类数据
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper.ne("parent_id","0");
        List<EduSubject> twoSubjects = baseMapper.selectList(wrapper2);
        //封装一级分类
        //创建总数据集合
        List<OneSubject> finalSubjectList = new ArrayList<>();
        for (int i = 0; i < oneSubjects.size(); i++) {
            //获取每一个一级分类的数据
            EduSubject eduSubject = oneSubjects.get(i);
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduSubject,oneSubject);
            finalSubjectList.add(oneSubject);
            List<TwoSubject> children = new ArrayList<>();
            for (int m = 0; m < twoSubjects.size(); m++) {
                //获取每一个二级分类的数据
                EduSubject eduSubject2 = twoSubjects.get(m);
                if(eduSubject2.getParentId().equals(eduSubject.getId())){
                    //创建二级类别vo对象
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    children.add(twoSubject);
                }
            }
            //封装二级分类
            oneSubject.setChildren(children);
        }
        return finalSubjectList;
    }
}
